package com.person.controller;

import com.person.model.entity.Result;
import com.person.service.ImportService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ImportController {
    @Resource
    private ImportService importService;

    @PostMapping("/import")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("请传入一个Excel文件");
        }
        // 检查文件扩展名
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            return Result.error("请传入一个Excel文件");
        }
        if (fileName.toLowerCase().endsWith(".xlsx") || fileName.toLowerCase().endsWith(".xls")) {
            importService.uploadFile(file);
            return Result.success();
        } else {
            return Result.error("请传入一个Excel文件");
        }
    }
}
